var express = require('express');
var router = express.Router();
var mysql = require("../db.js");

router.post('/countsum', function (req, res, next) {
  mysql.query(`select region,count(1) from  template_start  group by region`, (data) => {
    let arr = [];
    //console.log(data)
    for (let i in data) {
      //console.log(data[i]['region'])
      if (String(data[i]['region']).indexOf("区") > -1) {
        let obj = { genre: data[i]['region'], "学生数量": data[i]['count(1)'] }
        console.log(obj)
        arr.push(obj);
      }
    }
    console.log(arr);
    res.json(arr);
  })
})
router.post('/account', function (req, res, next) {
  mysql.query(`SELECT region,account,usertype from allbranch where usertype like "%${req.body.branch}%" order by id desc  `, (data) => {
    res.json(data);
  })
})
router.post('/reaccount', function (req, res, next) {
  mysql.query(`SELECT region,code,type from region_code where type like "%${req.body.branch}%" order by id desc  `, (data) => {
    res.json(data);
  })
})
router.post('/info', function (req, res, next) {
  mysql.query(`SELECT * from  template_start where cardcode like "%${req.body.cardcode}%" order by id desc limit 200  `, (data) => {
    res.json(data);
  })
})
router.post('/state', function (req, res, next) {
  console.log(req.body.filters)
  let filter = JSON.parse(req.body.filters);
  
  let filsql = "";
  console.log(filter['commonflag'])
  if (filter['commonflag']) {
    for (let i in filter['commonflag']) {
      if(i==0){
        filsql += ` and commonflag=${filter['commonflag'][i]}`
      }else{
        filsql += ` or commonflag=${filter['commonflag'][i]}`
      }
     
    }
  }
  console.log(filsql)
  if (filter['auditflag']) {
    for (let i in filter['auditflag']) {
      if(i==0){
        filsql += ` and auditflag=${filter['auditflag'][i]}`
      }else{
        filsql += ` or auditflag=${filter['auditflag'][i]}`
      }
    }
  }


  console.log(`SELECT * from  branch_code_start where branch like "%${req.body.branch}%" ${filsql} order by id desc`)
 
  mysql.query(`SELECT * from  branch_code_start where branch like "%${req.body.branch}%" ${filsql} order by id desc `, (data) => {
    res.json(data);
  })
})
module.exports = router;
